package com.dnjn.happiness.emo.controller;

import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dnjn.happiness.common.cloud.controller.BaseController;
import com.dnjn.happiness.common.core.utils.bean.BeanUtils;
import com.dnjn.happiness.common.core.web.domain.AjaxResult;
import com.dnjn.happiness.common.log.annotation.Log;
import com.dnjn.happiness.common.log.enums.BusinessType;
import com.dnjn.happiness.common.security.utils.SecurityUtils;
import com.dnjn.happiness.emo.domain.Ranklist;
import com.dnjn.happiness.emo.domain.vo.RanklistVO;
import com.dnjn.happiness.emo.service.IRanklistService;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.AllArgsConstructor;
import lombok.Getter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Arrays;

/**
 * 每日打卡榜单ranklistController
 *
 * @author amber
 * @date 2024-03-05
 */
@Tag(name = "emo ranklist 每日打卡榜单ranklist", description = "每日打卡榜单ranklist")
@RestController
@RequestMapping("/ranklist")
public class RanklistController extends BaseController {
    @Autowired
    private IRanklistService ranklistService;

    @AllArgsConstructor
    @Getter
    public enum RankType {
        TOTAL_COUNT("total_count"), LASTING_COUNT("lasting_count");
        private String col;
    }

    @GetMapping("/topN")
    @Log(title = "emo", businessType = BusinessType.LIST)
    public AjaxResult listTopN(@RequestParam(defaultValue = "20") Integer size, RankType type) {
        Page page = Page.of(1, size);
        page.setOrders(OrderItem.descs(type.getCol()));
        Page<RanklistVO> list = ranklistService.selectPage(page);
        return success(list);
    }


    /**
     * 查询每日打卡榜单ranklist列表
     */
    //@RequiresPermissions("@ss.hasPermi('emo:ranklist:list')")
    @PostMapping("/list")
    @Log(title = "emo", businessType = BusinessType.LIST)
    public AjaxResult list(@RequestBody RanklistVO vo, @RequestParam(defaultValue = "1") Integer current, @RequestParam(defaultValue = "10") Integer size) {
        Ranklist ranklist = new Ranklist();
        BeanUtils.copyPropsIgnoreNull(vo, ranklist);
        Page page = Page.of(current, size);
        Page<Ranklist> list = ranklistService.page(page, Wrappers.query(ranklist));
        return success(list);
    }

    /**
     * 获取每日打卡榜单ranklist详细信息
     */
    //@RequiresPermissions("@ss.hasPermi('emo:ranklist:query')")
    @GetMapping(value = "/{id}")
    @Log(title = "biz", businessType = BusinessType.SELECT)
    public AjaxResult get(@PathVariable("id") Long id) {
        return success(ranklistService.getById(id));
    }

    @GetMapping(value = "/getByUserId/{userId}")
    @Log(title = "biz", businessType = BusinessType.SELECT)
    public AjaxResult getByUserId(@PathVariable("userId") Long userId) {
        return success(ranklistService.getByUserId(userId));
    }


    /**
     * 新增每日打卡榜单ranklist
     */
    @Log(title = "每日打卡榜单ranklist", businessType = BusinessType.INSERT)
    //@RequiresPermissions("@ss.hasPermi('emo:ranklist:add')")
    @PostMapping("/add")
    public AjaxResult add(@RequestBody RanklistVO vo) {
        Ranklist ranklist = new Ranklist();
        BeanUtils.copyPropsIgnoreNull(vo, ranklist);

        ranklist.setCreateBy(SecurityUtils.getUsername());
        return toAjax(ranklistService.save(ranklist));
    }

    /**
     * 修改每日打卡榜单ranklist
     */
    @Log(title = "每日打卡榜单ranklist", businessType = BusinessType.UPDATE)
    //@RequiresPermissions("@ss.hasPermi('emo:ranklist:edit')")
    @PutMapping("/edit")
    public AjaxResult edit(@RequestBody RanklistVO vo) {
        Ranklist ranklist = new Ranklist();
        BeanUtils.copyPropsIgnoreNull(vo, ranklist);
        return toAjax(ranklistService.updateById(ranklist));
    }

    /**
     * 删除每日打卡榜单ranklist
     */
    @Log(title = "每日打卡榜单ranklist", businessType = BusinessType.DELETE)
    //@RequiresPermissions("@ss.hasPermi('emo:ranklist:remove')")
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(ranklistService.removeBatchByIds(Arrays.asList(ids)));
    }
}
